package com.edward.action;

import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.UnDb;
import com.edward.service.UnDbManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UnDbAction extends ActionSupport{

	private static final long serialVersionUID = -2796467861720626820L;
	
	private UnDb unDb;
	
	private UnDbManager unDbManager;
	
	private String code;
	
	private Date  submitDate;
	
	private String datas="";
	
	private int limit;
	
	private int start;
	
	private String flag = "";
	
	public Date getSubmitDate() {
		return submitDate;
	}

	public void setSubmitDate(Date submitDate) {
		this.submitDate = submitDate;
	}
	
	public String getDatas() {
		return datas;
	}

	public void setDatas(String datas) {
		this.datas = datas;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public UnDb getUnDb() {
		return unDb;
	}

	public void setUnDb(UnDb unDb) {
		this.unDb = unDb;
	}

	public void setUnDbManager(UnDbManager unDbManager) {
		this.unDbManager = unDbManager;
	}
	
	
	@Secured("ROLE_FINANCE")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		unDbManager.addUnDb(unDb);
		int total = unDbManager.listUnDb().size();
		response.getWriter().print("{success : true , msg : '添加成功!',unDb_id:"+ unDb.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UnDb u = unDbManager.getUnDb(unDb.getId());
		unDb.setTaxSum(u.getTaxSum());
		unDb.setItems(u.getItems());
		unDb.setTag(u.getTag());
		unDbManager.updateUnDb(unDb);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		UnDb u = unDbManager.getUnDb(unDb.getId());
		if(u.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在货物列表，请先删除货物!'}");
		}else{
			unDbManager.deleteUnDb(u.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = unDbManager.pagerList(start, limit);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}

	@Secured("ROLE_FINANCE")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		unDbManager.addDbItem(code,submitDate,unDb.getId());
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String addItems() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		
		if (datas.indexOf(",") < 0) {
			unDbManager.addDbItems(unDb,Integer.parseInt(datas));
		} else {
			String id[] = datas.split(",");
			for (int i = 0; i < id.length; i++) {
				unDbManager.addDbItems(unDb,Integer.parseInt(id[i]));
			}
		}
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String updateItem() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
			unDbManager.updateItem(code,submitDate);
			response.getWriter().print("{success : true , msg : '修改成功!'}");
			return null;
		}
	@Secured("ROLE_FINANCE")
	public String deleteItem() throws Exception {
	
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
			unDbManager.deleteItem(unDb.getId(),code);
			response.getWriter().print("{success : true , msg : '删除成功'}");
			return null;
		}
//	
	//获取grid 显示 的 分页信息
	@Secured("ROLE_FINANCE")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(flag.equals("look")){
			pager = unDbManager.pagerDbItem(start, limit,code);
		}else{
			pager = unDbManager.pagerDbItem(start, limit,unDb.getId());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
}
